package com.frkj.yuan.activity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.frkj.yuan.R;
import com.frkj.yuan.api.bean.PeizhenPersonBean;
import com.frkj.yuan.api.category.PeizhenPersonCategory;
import com.frkj.yuan.api.presenter.PeizhenPersonPreseneter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 作者：菲菲 on 2022/2/19 15:51
 * company：丰蓉信息科技有限公司
 */
public class YuyueSuccessAcitivity extends BaseActivity implements PeizhenPersonCategory.View {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.person_name)
    TextView mPersonName;
    @BindView(R.id.tating_number)
    TextView mRatNumber;
    @BindView(R.id.raging_ping)
    AppCompatRatingBar compatRatingBar;
    @BindView(R.id.person_msg)
    TextView mPersonMsg;
    @BindView(R.id.icon_iamge)
    ImageView mIconImage;
    private String orderId;
    private String mToken;
    private PeizhenPersonPreseneter mPresenter;
    private String mMobile;

    @Override
    public int getLayoutId() {
        return R.layout.activity_yuyue_success;
    }
    @Override
    public void initLayoutView() {
        orderId = getIntent().getStringExtra("orderId");
        mTitleMsg.setText("等待服务");
        FullScreenUtils.getInstance().fullScreen(YuyueSuccessAcitivity.this,true);
        mToken = (String) SPUtils.get(YuyueSuccessAcitivity.this, ConstantStaticUrl.mToken, "");
        mPresenter = new PeizhenPersonPreseneter(this);
        SetPersenter(mPresenter);
        mPresenter.getPeizhenPerson(mToken,orderId);
    }
    @OnClick({R.id.base_back,R.id.call_peosn})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.call_peosn:
                // 检查是否获得了权限
                if (ContextCompat.checkSelfPermission(YuyueSuccessAcitivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // 若没有获得授权，申请授权
                    if (ActivityCompat.shouldShowRequestPermissionRationale(YuyueSuccessAcitivity.this,
                            Manifest.permission.CALL_PHONE)) {
                        // 如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
                        //  如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
                        // 如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                        // 弹窗需要解释为何需要该权限，再次请求授权
                        // 帮跳转到该应用的设置界面，让用户手动授权
                        Intent call = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", YuyueSuccessAcitivity.this.getPackageName(), null);
                        call.setData(uri);
                        startActivity(call);
                    } else {
                        // 不需要解释为何需要该权限，直接请求授权
                        ActivityCompat.requestPermissions(YuyueSuccessAcitivity.this,
                                new String[]{Manifest.permission.CALL_PHONE},
                                0);
                    }
                } else {
                    // 已经获得授权，可以打电话
                    // 通过intent跳转到拨打电话的界面
                    Intent intent1 = new Intent();
                    intent1.setAction(Intent.ACTION_CALL);
                    Uri phoneNum = Uri.parse("tel:" +mMobile);
                    intent1.setData(phoneNum);
                    startActivity(intent1);
                }
                break;
        }
    }
    @Override
    public void getPeizhenPerson(PeizhenPersonBean personBean) {
        if (personBean.getCode()==ConstantStaticUrl.SUCCESS_CODE){
            if (personBean.getData()!=null){
                mPersonName.setText(personBean.getData().getPatientName());
                compatRatingBar.setRating(personBean.getData().getScore());
                mRatNumber.setText(String.valueOf(personBean.getData().getScore()));
                mMobile = personBean.getData().getMobile();
                mPersonMsg.setText("预约成功，陪诊员将于"+personBean.getData().getServiceDay()+"  "+personBean.getData().getPeriodStr()+"在【"+personBean.getData().getHospitalStr()+"】等您，祝您早日康复~");
                RequestOptions requestOptions = new RequestOptions();
                requestOptions.error(R.drawable.logo_circler_bg)
                        .circleCrop()
                        .skipMemoryCache(true)
                        .diskCacheStrategy( DiskCacheStrategy.NONE );
                Glide.with(YuyueSuccessAcitivity.this)
                        .load(personBean.getData().getAvatar())
                        .apply(requestOptions)
                        .into(mIconImage);
            }
        }
    }
    @Override
    public void SetPersenter(Object persenter) {

    }
    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(YuyueSuccessAcitivity.this,mErrorMsg);
    }
}
